<?php

/**
 * Well Framework
 * 
 * Contains Front Controller pattern class
 * 
 * @package     Well
 *
 * @author Valerii Lazarevych <valeriy.fremar@gmail.com>
 */

/**
 * Class CommandResolver
 * 
 * Command object Factory
 */
class CommandResolver
{
    /**
     * Contain absract Command class
     * 
     * @var type Command
     * @access private
     * @static
     */
    private static $base_cmd;
    
    /**
     * Contain index command obj
     * 
     * @var type Command
     */
    private static $default_cmd;
    
    /**
     * Initialize $base_cmd & $default_cmd
     */
    public function __construct()
    {
        if ( !self::$base_cmd ) {            
              self::$base_cmd      =   new ReflectionClass( 'Command' );
              self::$default_cmd   =   new IndexCommand();   }
    }
    
    /**
     * Return command obj depend on request cmd property
     * If cmd = '' return $default_cmd
     */
    public function getCommand(Request $Request)
    {                                        
        //get filepath of command class
        $cmd         =  $Request->getProperty('cmd') != '' ?  $Request->getProperty('cmd') : 'index';        
        $classname   =  ucfirst($cmd).'Command';                                
                
        //creat instance and return it
        $cmd_class  =   new ReflectionClass( $classname );        
        
        if ($cmd_class->isSubclassOf(self::$base_cmd)){      
                        
            return $cmd_class->newInstance();
            
        } else {                        
            $Request->addFeedback( 'Object command of '.$cmd.' have not been found' );
            return self::$default_cmd;
        }                
    }        
        
}
